package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.entity.MyInvestListBean;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;

/**
 * Created by android-adt on 2016/3/31.
 */
public class MyInvestUniversalListAdaper extends BaseAdapter {

    private Activity context;
    private LayoutInflater inflater;

    private List<MyInvestListBean.TermList> list;
    private MyInvestListBean.TermList termList;
    public void setList(List<MyInvestListBean.TermList> list) {
        this.list = list;
    }
    public MyInvestUniversalListAdaper(Activity context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //普通列表项
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater.inflate(R.layout.item_my_fixed, null);
            holder = new ViewHolder();
            holder.tv_item_title = (TextView) convertView.findViewById(R.id.tv_item_title);
            holder.zhuan = (TextView) convertView.findViewById(R.id.zhuan);
            holder.quan = (TextView) convertView.findViewById(R.id.quan);
            holder.tv_touzibenjin = (TextView) convertView.findViewById(R.id.tv_touzibenjin);
            holder.tv_item_state = (TextView) convertView.findViewById(R.id.tv_item_state);
            holder.tv_invest_principal = (TextView) convertView.findViewById(R.id.tv_invest_principal);
            holder.tv_qiwang = (TextView) convertView.findViewById(R.id.tv_qiwang);
            holder.tv_wait_money = (TextView) convertView.findViewById(R.id.tv_wait_money);
            holder.tv_time_income0 = (TextView) convertView.findViewById(R.id.tv_time_income0);
            holder.tv_time_income1 = (TextView) convertView.findViewById(R.id.tv_time_income1);
            holder.tv_red_jindu = (TextView) convertView.findViewById(R.id.tv_red_jindu);
            holder.img_fengexian = (ImageView) convertView.findViewById(R.id.img_fengexian);
            holder.hongsejindu = (RelativeLayout) convertView.findViewById(R.id.hongsejindu);
            holder.numberbar = (cn.com.xinjinfu.pdoulcProduct.view.NumberProgressBar) convertView.findViewById(R.id.numberbar);
            holder.timedownview = (cn.com.xinjinfu.pdoulcProduct.view.TimeDownView) convertView.findViewById(R.id.timedownview);

            convertView.setTag(holder);//绑定ViewHolder对象

        } else {
            holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }

        termList = list.get(position);
        L.d("----------" + termList.toString());
        if (termList != null) {
            if (termList.getBidStatus() == 3) {                 //转让中（转让）
                if (termList.getBidProgress() == 100){
                    holder.tv_item_state.setText("已满标");
                }else {
                    holder.tv_item_state.setText("转让中");}
                holder.tv_touzibenjin .setText("投资本金");
                holder.img_fengexian.setVisibility(View.VISIBLE);
                holder.zhuan.setVisibility(View.GONE);
                holder.numberbar.setVisibility(View.VISIBLE);
                holder.timedownview.setVisibility(View.VISIBLE);
                holder.hongsejindu.setVisibility(View.GONE);
                daoJiShi(holder);
                holder.tv_qiwang.setText("待收金额");
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidWaitAmount()));
                holder.tv_wait_money.setTextColor(0xFFF77F96);
                holder.tv_time_income1.setText("已收金额");
                holder.tv_time_income0.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()));
                holder.numberbar.setProgress((int)Math.floor(termList.getBidProgress()));
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
                holder.numberbar.setReachedBarColor(0xffF4B942);
            } else if (termList.getBidStatus() == 1) {            //定期
                holder.tv_item_state.setText("投标中");
                holder.tv_touzibenjin .setText("投资本金");
                holder.numberbar.setVisibility(View.VISIBLE);
                holder.zhuan.setVisibility(View.GONE);
                holder.timedownview.setVisibility(View.GONE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.GONE);
                holder.numberbar.setProgress((int)Math.floor(termList.getBidProgress()));
                holder.tv_time_income0.setText(termList.getBidPeriod());
                holder.tv_time_income1.setText("期限");
                holder.tv_qiwang.setText("期望收益");
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidExpectEarn()));
                holder.tv_wait_money.setTextColor(0xFF6ED3D6);
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
            } else if (termList.getBidStatus() == 2) {            //定期
                holder.timedownview.setVisibility(View.GONE);
                holder.zhuan.setVisibility(View.GONE);
                holder.numberbar.setVisibility(View.VISIBLE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.GONE);
                holder.tv_item_state.setText("待放款");
                holder.tv_time_income0.setText(termList.getBidPeriod());
                holder.numberbar.setProgress((int)Math.floor(termList.getBidProgress()));
                holder.tv_time_income1.setText("期限");
                holder.tv_qiwang.setText("期望收益");
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidExpectEarn()));
                holder.tv_wait_money.setTextColor(0xFF6ED3D6);
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
            } else if (termList.getBidStatus() == 4) {                             //已转出
                holder.timedownview.setVisibility(View.GONE);
                holder.numberbar.setVisibility(View.GONE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.GONE);
                holder.zhuan.setVisibility(View.GONE);
                holder.tv_item_state.setText("已转出");
                holder.tv_touzibenjin .setText("投资金额");
                holder.tv_qiwang.setText("累计收益");
                holder.tv_wait_money.setTextColor(0xFF333333);
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()-
                        termList.getBidInvestAmount()));
                holder.tv_time_income1.setText("已收金额");
                holder.tv_time_income0.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()));
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
            } else if (termList.getBidStatus() == 5) {                             //回款中
                holder.timedownview.setVisibility(View.GONE);
                holder.numberbar.setVisibility(View.GONE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.VISIBLE);
                if (termList.getTransferNumber() > 0) {                     //转让
                    holder.zhuan.setVisibility(View.VISIBLE);
                }else {
                    holder.zhuan.setVisibility(View.GONE);                  //定期
                }

                holder.tv_item_state.setText("第" + (termList.getAlreadyRepaymentNper() + 1) + "/" +
                        termList.getTotalRepaymentNper() + "期");
                holder.tv_red_jindu.setText("第"+ (termList.getAlreadyRepaymentNper() + 1) + "期待收收益"
                        +TextViewUtil.getSumLength(termList.getNextRepaymentAmount()) + "元");
                holder.tv_qiwang.setText("待收金额");
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidWaitAmount()));
                holder.tv_wait_money.setTextColor(0xFF6ED3D6);
                holder.tv_time_income1.setText("已收金额");
                holder.tv_time_income0.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()));
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
            } else if (termList.getBidStatus() == 6) {                             //已结束
                holder.timedownview.setVisibility(View.GONE);
                holder.tv_item_state.setText("已结束");
                holder.numberbar.setVisibility(View.GONE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.GONE);
                if (termList.getBidfirstOrreturn() == 1) {                  //转让
                    holder.zhuan.setVisibility(View.VISIBLE);
                } else {
                    holder.zhuan.setVisibility(View.GONE);                  //定期
                }
                holder.tv_touzibenjin .setText("投资金额");
                holder.tv_qiwang.setText("累计收益");
                holder.tv_wait_money.setTextColor(0xFF333333);
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()-
                        termList.getBidInvestAmount()));
                holder.tv_time_income1.setText("已收金额");
                holder.tv_time_income0.setText(TextViewUtil.getSumLength(termList.getBidAlreadyAmount()));
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getBidInvestAmount()));
            }else if (termList.getBidStatus() == 7) {
                holder.zhuan.setVisibility(View.VISIBLE);
                holder.numberbar.setVisibility(View.VISIBLE);
                holder.timedownview.setVisibility(View.GONE);
                holder.img_fengexian.setVisibility(View.GONE);
                holder.hongsejindu.setVisibility(View.GONE);
                if (termList.getBidProgress() == 100){
                    holder.tv_item_state.setText("已满标");
                }else {
                    holder.tv_item_state.setText("投标中");
                }
                holder.numberbar.setProgress((int)Math.floor(termList.getBidProgress()));
                holder.tv_time_income0.setText(termList.getRemainTerm() + "天");
                holder.tv_touzibenjin .setText("购买价格");
                holder.tv_time_income1.setText("剩余期限");
                holder.tv_qiwang.setText("原始债权价值");
                holder.tv_wait_money.setText(TextViewUtil.getSumLength(termList.getOldInvestPrice()));
                holder.tv_wait_money.setTextColor(0xFF6ED3D6);
                holder.tv_invest_principal.setText(TextViewUtil.getSumLength(termList.getInvestPrice()));
            }
                holder.tv_item_title.setText(termList.getBidTitle());
            if (termList.getHavCoupons() > 0) {                     //券
                holder.quan.setVisibility(View.VISIBLE);
            }else {
                holder.quan.setVisibility(View.GONE);                  //非券
            }

        }
        return convertView;
    }
    /**
     * ****************  倒计时 **************
     */
    public void daoJiShi(ViewHolder holder) {
        long diff = termList.getTransferTime()/1000;
        L.d("MyFixedActivity倒计时===============================" + diff);
        long[] time;
        if (diff <= 0) {
            time = new long[]{0, 0, 0};
        } else {
            long hours = diff / (60 * 60);
            long minutes = (diff%(60 * 60)) / 60;
            long second = diff%(60 * 60) % 60;
            time = new long[]{hours, minutes, second};
        }
        holder.timedownview.setTimes(time);
        if (!holder.timedownview.isRun()) {
            holder.timedownview.run();
        }

    }

    public final class ViewHolder {
        public TextView tv_item_title;
        public TextView zhuan;
        public TextView quan;
        public TextView tv_item_state;
        public TextView tv_invest_principal;
        public TextView tv_touzibenjin;
        public TextView tv_qiwang;
        public TextView tv_wait_money;
        public TextView tv_time_income0;
        public TextView tv_time_income1;
        public TextView tv_red_jindu;
        public ImageView img_fengexian;
        public RelativeLayout hongsejindu;
        public cn.com.xinjinfu.pdoulcProduct.view.NumberProgressBar numberbar;
        public cn.com.xinjinfu.pdoulcProduct.view.TimeDownView timedownview;
    }
}
